module CodeGraphs
  include DrunkenMaster
  require 'json'
  
  class Application < Widget

    state_defined_by :tail => '/trunk', :graph_type => :inheritance
    
    def initialize
      super()
      @root = File.dirname(__FILE__)+'/../../..'
    end
    
    def index
      e :h1, "CodeGraphs"
      e :div, :id => "header"
      e :div, :id => 'infovis'
      #e :div, :id => 'log', :style => 'display:none'
      
      dpicker = add DirectoryPicker.new(@root)
      dpicker.folder_blacklist.add(/test/)
      dpicker.folder_blacklist.add(/doc/)
      dpicker.folder_blacklist.add(/nbproject/)
      
      dpicker.style = "position:absolute;top:0;left:0;color:#ddd;text-align:left;"
      dpicker.items.each do |item|
        item.onclick = "jQuery.getScript('#{callback :set_root}#{item.file}')"
      end
      
      e :form, :style => 'position:absolute;left:10em;top:0;color:#eee;background-color:#404040;line-height:220%;padding-right:0.6em' do
        [:inheritance, :instantiation, :method_usage].each do |type|
          radio = 
          e :input,
            :type => 'radio', 
            :name => 'graph_type', 
            :value => 'inheritance', 
            :onclick => "jQuery.getScript('#{callback :set_type}/#{type}')"
          if graph_type == type
            radio.checked = 'checked'
          end

          e :label, :for => radio.id, :innerHTML => type.to_s
        end
      end
    end
    
    def set_root tail
      self.tail = tail
      respond(" jQuery('#mycanvas-label').html('');
                init(#{data}, null);")
    end
    
    def set_type type
      self.graph_type = type[0].to_sym
      respond(" jQuery('#mycanvas-label').html('');
                init(#{data}, null);")
    end
    
    def data
      CodeDependencies.new(File.join(@root, tail)).send(graph_type)
    end


    def render
      context.page.link_css file_url('jit/css/style.css')
      context.page.link_css file_url('jit/css/infovis.css')
      context.page.link_css file_url('jit/css/rgraph.css')
#      context.page.link_css file_url('jit/css/hypertree.css')
#      context.page.link_css file_url('jit/css/spacetree.css')
#      context.page.link_css file_url('jit/css/treemap.css')
      context.page.link_css file_url('style.css')

      context.page.link_js file_url('jquery-1.2.6.js')
      context.page.link_js file_url('jquery-ui-personalized-1.5.3.js')
      context.page.head.e :script, "jQuery.noConflict(); jQuery(function($) { $('#infovis').draggable(); })"

      context.page.link_js file_url('jit/js/extras/mootools-1.2.js')
      context.page.head.e nil,
      '<!--[if IE]><script language="javascript" type="text/javascript" src="'+file_url('jit/js/extras/excanvas.js')+'"></script><![endif]-->'
      context.page.link_js file_url('jit/js/Hypertree.js')
      context.page.link_js file_url('jit/js/RGraph.js')
      context.page.link_js file_url('jit/js/Spacetree.js')
      context.page.link_js file_url('jit/js/Treemap.js')
      context.page.link_js file_url('infovis.js')
      context.page.link_js file_url('static2-rgraph.js')
      
      context.page.body.onload = "init(#{data.gsub(/"/, "&quot;")}, null);"
      super()
    end
  end
end

=begin
Graph data syntax:

'[
   {
    "id": "node0",
    "name":"node0 name",
    "data":[
      {"key":"weight","value":16.759175934208628},
      {"key":"some other key","value":"some other value"}
    ],
    "adjacencies":[
      {"nodeTo":"node1","data":{"weight":3}},
      {"nodeTo":"node2","data":{"weight":3}},
      {"nodeTo":"node3","data":{"weight":3}},
      {"nodeTo":"node4","data":{"weight":1}},
      {"nodeTo":"node5","data":{"weight":1}}
    ]
  },
  ...
=end      